﻿using Coolzon.UI.Core.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Core.Converters
{
    public class BrushListConverter : ValueConverter
    {
        public object Convert(ConvertContext context, RT.Property property)
        {
            BrushList colorPlate = new BrushList();
            if (string.Equals("BrushList", property.ValueNode.Name))
            {
                foreach (RT.ValueNode valueNode in property.ValueNode.ValueNodes)
                {
                    colorPlate.Add(BrushConverter.ConvertFromValueNode(context, valueNode));
                }
            }
            return colorPlate;
        }

        public RT.ValueNode ConvertBack(ConvertContext context, object value, object dtTagData)
        {
            BrushList colorPlate = (BrushList)value;
            if (colorPlate == null)
            {
                return null;
            }

            RT.ValueNode colorPlateRootNode = new RT.ValueNode("ColorPlate");
            colorPlateRootNode.Attributes = new Dictionary<string, string>();
            colorPlateRootNode.ValueNodes = new List<RT.ValueNode>();
            
            if (colorPlate.Count > 0)
            {
                for (int index = 0; index < colorPlate.Count; index++)
                {
                    RT.ValueNode vNode = new RT.ValueNode("SolidBrush");
                    vNode.Attributes = new Dictionary<string, string>();
                    Color color = (Color)ColorConverter.ConvertFromString(System.Convert.ToString(colorPlate[index]));
                    string colorString = color.ToString();
                    vNode.Attributes.Add("Color", colorString);
                    colorPlateRootNode.ValueNodes.Add(vNode);
                }
            }

            return colorPlateRootNode;
        }
    }
}
